import request from '@/utils/request.js'
import config from '@/utils/config/index.js'

/**
 * 获取待审批数据
 */
export function getPendingTasks(pager, params, loading = true) {
	return request.post({
		// url: '/business/v3/bpm/received/query/pending/user',
		url: '/extend/v3/bpm/received/query/pending/userExtend',
		pager,
		params,
		paramType: 'post',
		type: 'platform',
		loading: loading
	});
}

/**
 * 获取已审批数据
 */
export function getHandledTasks(pager, params) {
	return request.post({
		url: '/business/v3/bpm/received/query/handled',
		pager,
		params,
		type: 'platform'
	});
}

/**
 * 获取我发起的数据
 */
export function getInitiateTasks(pager, params) {
	return request.post({
		url: '/business/v3/bpm/initiated/query/my/requested',
		pager,
		params,
		type: 'platform'
	});
}

/**
 * 获取流程定义数据
 */
export function getFlowDefinition() {
	let params = []
	params.push({
		key: "Q^status_^S",
		value: "deploy"
	})
	params.push({
		key: "isSuper",
		value: true
	})
	
	return request.post({
		url: '/business/v3/bpm/definition/query',
		params,
		type: 'platform'
	});
}

/**
 * 启动流程
 */
export async function startFlow(query) {
	return request.post({
		url: '/business/v3/bpm/instance/start',
		params: query,
		type: 'platform'
	});
}

/**
 * 启动流程
 */
export async function startFlowFromList(params, data) {
	let url = "/business/v3/bpm/instance/startFlowFromList"
	let parameter = ''
	for (const name in params) {
		if(params[name] !== undefined && params[name] !== null)
			parameter += `&${name}=${params[name]}`;
	}
	
	if (parameter !== '') {
		parameter = parameter.substr(1);
		url += url.includes('?') ? '' : '?' + parameter;
	}
	
	let result = await uni.request({
		url: config.baseUrl + url,
		data: data,
		method: 'POST',
		header: {
			'X-Authorization-access_token': uni.getStorageSync("_app_token")
		}
	})
	return result[1].data
}

/**
 * 启动流程
 */
export async function StartFlowNew(query) {
	uni.showLoading({ title:"请稍后", mask: true })
	let authToken = uni.getStorageSync("_app_token")
	let result = await uni.request({
		url: `${config.baseUrl}/business/v3/bpm/instance/startFlowFromList?defKey=${query.defKey}&formKey=${query.formKey}&ids=${query.ids}`,
		data: query.nodeUsers,
		paramType: "post",
		method: 'POST',
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'X-Authorization-access_token': authToken
		}
	})
	uni.hideLoading()
	return result[1].data
}

/**
 * 启动流程
 */
export async function StartFlowByVariable(query) {
	uni.showLoading({ title:"请稍后", mask: true })
	let authToken = uni.getStorageSync("_app_token")
	let result = await uni.request({
		url: `${config.baseUrl}/business/v3/bpm/instance/startFlowFromListExpand?defKey=${ query.defKey }&formKey=${ query.formKey }&ids=${ query.ids }&destination=${query.destination}&flowParam=${ query.flowParam }`,
		data: query.nodeUsers,
		method: 'POST',
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'X-Authorization-access_token': authToken
		}
	})
	uni.hideLoading()
	return result[1].data
}

/**
 * 启动流程
 */
export async function startFlowExpand(query) {
	uni.showLoading({ title:"请稍后", mask: true })
	let authToken = uni.getStorageSync("_app_token")
	let result = await uni.request({
		url: `${config.baseUrl}/business/v3/bpm/instance/startFlowFromListExpand?defKey=${ query.defKey }&formKey=${ query.formKey }&ids=${ query.ids }&flowParam=${ query.flowParam }`,
		data: query.nodeUsers,
		method: 'POST',
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'X-Authorization-access_token': authToken
		}
	})
	uni.hideLoading()
	return result[1].data
}

/**
 * 启动流程
 */
export function cpisStartFlow(query) {
	return request.post({
		url: '/business/v3/bpm/instance/cpisStartFlow',
		params: query,
		paramType: 'post'
	});
}

/**
 * 流程-同意
 */
export function agreeFlow(params) {
	return request.post({
		url: '/business/v3/bpm/task/agree',
		params,
		paramType: 'post'
	});
}

/**
 * 流程-驳回上一步
 */
export function previous(params) {
	return request.post({
		url: '/business/v3/bpm/task/reject/previous',
		params,
		paramType: 'post'
	})
}

/**
 * 流程-驳回发起人
 */
export function backStarter(params) {
	return request.post({
		url: '/business/v3/bpm/task/reject/starter',
		params,
		paramType: 'post'
	});
}

/**
 * 流程-驳回
 */
export function rejectFlow(params){
	return request.post({
		url   : '/business/v3/bpm/task/reject',
		params,
		paramType: 'post'
	})
}

/**
 * 流程-反对
 */
export function opposeFlow(params) {
	return request.post({
		url: '/business/v3/bpm/task/oppose',
		params,
		paramType: 'post'
	});
}

/**
 * 流程-终止
 */
export function endFlow(params) {
	return request.post({
		url: '/business/v3/bpm/task/doEndProcess',
		params
	});
}

/**
 * 流程-获取任务办理数据
 */
export function getAgreeData(taskId, actionName) {
	const params = {
		taskId: taskId,
		actionName: actionName
	}
	return request.post({
		url: '/business/v3/bpm/task/agreeData/vo',
		params,
		paramType: 'post'
	});
}

/**
 * 流程-获取任务表单的详情(根据taskId)
 */
export function getFormData(taskId) {
	const params = {
		taskId: taskId
	}
	return request.get({
		url: '/business/v3/bpm/task/getFormData',
		params
	});
}

/**
 * 流程-获取任务表单的详情(根据instId)
 */
export function getInstIdFormData(instId) {
	const params = {
		instId: instId
	}
	return request.get({
		url: '/business/v3/bpm/task/getInstIdFormData',
		params
	});
}

/**
 * 流程-获取任务的详情
 */
export function getTaskInfo(taskId) {
	const params = {
		id: taskId,
	}
	return request.get({
		url: '/business/v3/bpm/task/get',
		params
	});
}

/**
 * 流程-获取流程实例数据
 */
export function getFormInfo(instId) {
	const params = {
		instId: instId,
	}
	return request.get({
		url: '/business/v3/bpm/instance/instFormInfo',
		params
	});
}

/**
 * 根据业务主键获取流程信息
 */
export function getFlowInfo(bizKey) {
	const params = {
		bizKey
	}
	return request.get({
		url: '/extend/v3/bpm/received/getByDefKeyBizKey',
		params
	});
}

/**
 * 获取流程审批历史
 */
export function getFlowHistory(params) {
	return request.post({
		url: '/business/v3/bpm/instance/flowHistory',
		params,
		paramType: 'post'
	});
}

/**
 * 获取流程待办信息（效率低，不再使用）
 */
export function getFlowTasks() {
	const params = []
	params.push({
		key: "key",
		value: "wddbst"
	})
	params.push({
		key: "Q^executor_^S",
		value: uni.getStorageSync("userId")
	})
	return request.post({
		url: '/business/v3/data/template/queryDataByKey',
		params,
		type: 'platform',
		paramType: 'post'
	});
}

/**
 * 获取流程实例信息
 */
export function getFlowInstance(id) {
	const params = {
		id
	}
	
	return request.get({
		url: '/business/v3/bpm/instance/get',
		params,
		loading: false
	});
}

/**
 * 获取流程任务变更信息
 */
export function getTaskChange(taskId) {
	const params = {
		taskId
	}
	
	return request.get({
		url: '/business/v3/bpm/task/change/edit',
		params
	});
}

/**
 * 保存流程任务变更信息
 */
export function saveTaskChange(params) {
	return request.post({
		url: '/business/v3/bpm/task/change/save',
		params,
		paramType: 'post'
	});
}

/**
 * 流程-获取初始化脚本
 */
export function getInitScript(taskId) {
	return request.get({
		url: '/business/v3/bpm/definition/settingByScript',
		params: { taskId }
	});
}

/**
 * 根据defkey获取初始化脚本及按钮脚本
 */
export function getStartScript(defKey) {
	return request.get({
		url: '/business/v3/bpm/definition/getStartScript',
		params: { defKey }
	});
}